<?php
include_once("include/config.php");
include_once("include/auth.lib.php");
include_once("include/utils.lib.php");
include_once("include/license.lib.php");
include_once("include/reg.lib.php");

function html_header($title){
global $msg,$logged;
list($status, $user) = auth_get_status();
//print_r($user); //DEBUG check user data after login
echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n"; 
echo "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n"; 
echo "<head>\n"; 
echo "<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />\n"; 
echo "<title>".$title."</title>\n"; 
echo "<meta name=\"keywords\" content=\"cool help desk,helpdesk,supporto,ticket\" />\n"; 
echo "<meta name=\"description\" content=\"\" />\n"; 
echo "<meta name=\"generator\" content=\"Me\" />\n"; 
echo "<link href=\"css/default.css\" rel=\"stylesheet\" type=\"text/css\" />\n";
echo "<script type=\"text/javascript\" src=\"js/mootools.js\"></script> \n 
<script type=\"text/javascript\" src=\"js/moodalbox.js\"></script>\n";
echo "</head>\n"; 
echo "<body>\n"; 
echo "<div id=\"header\">\n"; 
echo "	<h1><a href=\"index.php\" class=\"none\">COOL HELPDESK</a></h1><br>"; 
echo "</div>\n"; 
echo "<div id=\"menu\">\n"; 
echo "	<ul>\n"; 
if($status == AUTH_LOGGED)
echo "		<li class=\"first\"><a href=\"index.php\">Home</a></li>\n"; 
echo "		<li><a href=\"users.php\">User</a></li>\n"; 
echo "		<li><a href=\"support.php\">Support</a></li>\n"; 
echo "		<li><a href=\"admin.php\">Administrator</a></li>\n"; 
echo "		<li><a href=\"about.php\">About</a></li>\n"; 
if($status == AUTH_LOGGED )
echo"       <li><b>".$user['username']."<a href=\"logout.php\" rel=\"moodalbox 310 110\"> [Logout]</a></b></li>\n";
else{
echo "		<li><a href=\"login.php\" rel=\"moodalbox 295 90\" title=\"Login page\">Login</a></li>\n"; 
echo "      <li><a href=\"registration.php\" rel=\"moodalbox\" title=\"Registration\">Register</a></li>\n"; 
}
echo "	</ul>\n"; 
echo "</div>\n"; 
echo "<div id=\"content\">\n"; 
echo "<div id=\"columnA\">\n";
if(isset($msg)){
echo "<div class='box'>\n"; 
echo " <div class='boxtop'><div></div></div>\n"; 
echo "  <div class='boxcontent'>\n"; 
echo $msg;
echo "  </div>\n"; 
echo " <div class='boxbottom'><div></div></div>\n"; 
echo "</div>\n"; 
echo "\n";
}
}


function html_footer(){
echo "</div>\n"; 
echo "	<div id=\"columnB\">\n"; 
echo "	</div>\n"; 
echo "	<div style=\"clear: both;\">&nbsp;</div>\n"; 
echo "</div>\n"; 
echo "<div id=\"footer\">\n"; 
echo "<p>Cool helpdesk 2008 by <a href=\"mailto:maze04@libero.it\">MaZe</a>\n"; 
echo "</p>";
echo "</div>\n"; 
echo "</body>\n"; 
echo "</html>\n";
}

function login($uname,$passwd){
global $msg,$logged;
list($status, $user) = auth_get_status();
if($status == AUTH_NOT_LOGGED){
	$uname = strtolower(trim($_POST['uname']));
	$passw = strtolower(trim($_POST['passw']));

	if($uname == "" or $passw == ""){
		$status = AUTH_INVALID_PARAMS;
	}else{
		list($status, $user) = auth_login($uname, $passw);
		if(!is_null($user)){
			list($status, $uid) = auth_register_session($user);
		}
	}
}

switch($status){
	case AUTH_LOGGED:
		header("Refresh: 2;URL=index.php");
		$msg='<div align="center">You are already connected</div>';
	break;
	case AUTH_INVALID_PARAMS:
		header("Refresh: 3;URL=index.php");
		$msg='<div align="center"><h2>You have specified an incorrect username or password. Please check your data and try again.</h2></div>';
	break;
	case AUTH_LOGEDD_IN:
		switch(auth_get_option("TRANSICTION METHOD")){
			case AUTH_USE_LINK:
				header("Refresh: 2;URL=index.php?uid=".$uid);
				$msg='<a href="index.php?uid='.$uid.'>Login Link</a>';
			break;
			case AUTH_USE_COOKIE:
				//header("Refresh: 2;URL=index.php");
				setcookie('uid', $uid, time()+3600*365);
			break;
			case AUTH_USE_SESSION:
				//header("Refresh: 2;URL=index.php");
				$_SESSION['uid'] = $uid;
			break;
		}
		if($status='AUTH_USE_LINK')
		$msg='<a href="index.php?uid='.$uid.'>Login Link</a>';
		
		$msg='<div align="center"><h3>Welcome '.$user['username'].' </h3></div>';
		
		$logged=TRUE;
	break;
	case AUTH_FAILED:
		//header("Refresh: 2;URL=index.php");
		$msg='<div align="center"><h3>Connection failed</h3></div>';
	break;
}}



?>